public without sharing class MaccuraAuthorizationHandler {
    
    ///所有的产品线和等级
    public static void allProductlineAndLevel(Set<Id> accSet ){
    	Map<Id,Set<String>> prodLevelMap = new Map<Id,Set<String>>();
        List<String> years = new List<String>();
        List<MaccuraAuthorization__c> maList = [select Id,ProductLine__c,ProxyGrade__c,Year__c ,MaccuraDealer__c from MaccuraAuthorization__c 
            where MaccuraBrand__c in: MaccuraBrandUtil.maccura and MaccuraDealer__c in: accSet ];
           
        Map<String,String>yearMap = getMaxYear(maList);
        //if(yearMap.isEmpty()) return;
        System.debug(yearMap);

    	for(MaccuraAuthorization__c ma :maList ){

            if(ma.Year__c == null || yearMap.get(ma.MaccuraDealer__c) == null  ||  !ma.Year__c.contains(yearMap.get(ma.MaccuraDealer__c)) ) continue;
    		Set<String>prodLevelSet = prodLevelMap.containsKey(ma.MaccuraDealer__c )?prodLevelMap.get(ma.MaccuraDealer__c) : new Set<String>();

    		prodLevelSet.add(ma.ProductLine__c+ma.ProxyGrade__c);
    		prodLevelMap.put(ma.MaccuraDealer__c, prodLevelSet);
    	}
        System.debug(prodLevelMap);
    	List<Account> accList = new List<Account>();

    	for(Id accId : accSet  ){
            //prodLevelMap.get(accId)
            List<String >proList = prodLevelMap.containsKey(accId) ? new List<String>(prodLevelMap.get(accId) )  : new List<String>();
            String m = prodLevelMap.containsKey(accId) ? String.join(proList, ';') : null;
    		accList.add(new Account(Id=accId, MaccuraAuthorization__c = m ) );
    	}
    	if(!accList.isEmpty()) update accList;

    }

    ///删除 或 新增
	public static void  upgradeToAccount (List<MaccuraAuthorization__c> plist){
		Set<Id> proset = new Set<Id>();
		for (MaccuraAuthorization__c p : plist) {
			if(p.MaccuraDealer__c != null )proset.add(p.MaccuraDealer__c);
		}
		if( proset.size() > 0)allProductlineAndLevel(proset);
		
	}
    //获取最新的年份
    private static Map<String,String>  getMaxYear(List<MaccuraAuthorization__c> mlist){

        
        Map<String,List<String> > myMap =new Map<String,List<String>>();
        for(MaccuraAuthorization__c ma : mlist){
            List<String> pList =myMap.containsKey(ma.MaccuraDealer__c)?myMap.get(ma.MaccuraDealer__c) : new List<String>();
            if(ma.Year__c != null )pList.add(ma.Year__c);
            myMap.put(ma.MaccuraDealer__c, pList);


        }

        Map<String ,List<Integer>> yearMap =new Map<String  ,List<Integer>>();
        for(String acckey : myMap.keySet()){
           for(String y : myMap.get(acckey) ){
                Integer year = null;
                try {

                    if(y != null && y.length() > 4 ){
                        year = Integer.valueOf(y.substring(0,4));
                        List<Integer> pList = yearMap.containsKey(acckey) ? yearMap.get(acckey) : new List<Integer>();

                        pList.add(year);

                        yearMap.put(acckey, pList);
                    }

                }catch (Exception e) {}

                
            }

        }
        Map<String,String> resMap =new Map<String,String>();
        for (String acckey : yearMap.keySet() ) {
            
             List<Integer> pList = yearMap.get(acckey);

             if(!pList.isEmpty())pList.sort();
             String year = pList == null || pList.isEmpty() ? null : String.valueOf(pList.get(pList.size() -1 ));

             resMap.put(acckey, year);
        }
    

        return resMap;
    }



}